namespace AonesoftBase.DBUtility
{
    using AonesoftBase.Biz;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;

    public abstract class DbHelperSQL
    {
        private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
            return command;
        }

        private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            SqlCommand command = new SqlCommand(storedProcName, connection);
            command.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter parameter in parameters)
            {
                if (parameter != null)
                {
                    if (((parameter.Direction == ParameterDirection.InputOutput) || (parameter.Direction == ParameterDirection.Input)) && (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    command.Parameters.Add(parameter);
                }
            }
            return command;
        }

        public static SqlDataReader ExecuteReader(string conn, string strSQL)
        {
            SqlDataReader reader2;
            SqlConnection connection = new SqlConnection(conn);
            SqlCommand command = new SqlCommand(strSQL, connection);
            try
            {
                connection.Open();
                reader2 = command.ExecuteReader();
            }
            catch (SqlException exception)
            {
                throw new Exception(exception.Message);
            }
            return reader2;
        }

        public static SqlDataReader ExecuteReader(string conn, string SQLString, params SqlParameter[] cmdParms)
        {
            SqlDataReader reader2;
            SqlConnection connection = new SqlConnection(conn);
            SqlCommand cmd = new SqlCommand();
            try
            {
                PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                SqlDataReader reader = cmd.ExecuteReader();
                cmd.Parameters.Clear();
                reader2 = reader;
            }
            catch (SqlException exception)
            {
                throw new Exception(exception.Message);
            }
            return reader2;
        }

        public static int ExecuteScalar(string conn, string SQLString)
        {
            int num2;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                using (SqlCommand command = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        num2 = Convert.ToInt32(command.ExecuteScalar());
                    }
                    catch (SqlException exception)
                    {
                        throw new Exception(exception.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return num2;
        }

        public static int ExecuteSql(string conn, string SQLString)
        {
            int num2;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                using (SqlCommand command = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        command.CommandTimeout = 0xfa0;
                        num2 = command.ExecuteNonQuery();
                    }
                    catch (SqlException exception)
                    {
                        throw new Exception(exception.Message);
                    }
                    finally
                    {
                        connection.Close();
                        command.Dispose();
                    }
                }
            }
            return num2;
        }

        public static int ExecuteSql(string conn, string SQLString, params SqlParameter[] cmdParms)
        {
            int num2;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                using (SqlCommand command = new SqlCommand())
                {
                    try
                    {
                        PrepareCommand(command, connection, null, SQLString, cmdParms);
                        int num = command.ExecuteNonQuery();
                        command.Parameters.Clear();
                        num2 = num;
                    }
                    catch (SqlException exception)
                    {
                        throw new Exception(exception.Message);
                    }
                    finally
                    {
                        connection.Close();
                        command.Dispose();
                    }
                }
            }
            return num2;
        }

        public static int ExecuteSql(string conn, string SQLString, string content)
        {
            int num2;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                SqlCommand command = new SqlCommand(SQLString, connection);
                SqlParameter parameter = new SqlParameter("@content", SqlDbType.NText);
                parameter.Value = content;
                command.Parameters.Add(parameter);
                try
                {
                    connection.Open();
                    num2 = command.ExecuteNonQuery();
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    command.Dispose();
                    connection.Close();
                }
            }
            return num2;
        }

        public static int ExecuteSqlByTime(string conn, string SQLString, int Times)
        {
            int num2;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                using (SqlCommand command = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        command.CommandTimeout = Times;
                        num2 = command.ExecuteNonQuery();
                    }
                    catch (SqlException exception)
                    {
                        throw new Exception(exception.Message);
                    }
                    finally
                    {
                        connection.Close();
                        command.Dispose();
                    }
                }
            }
            return num2;
        }

        public static object ExecuteSqlGet(string conn, string SQLString, string content)
        {
            object obj3;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                SqlCommand command = new SqlCommand(SQLString, connection);
                SqlParameter parameter = new SqlParameter("@content", SqlDbType.NText);
                parameter.Value = content;
                command.Parameters.Add(parameter);
                try
                {
                    connection.Open();
                    object objA = command.ExecuteScalar();
                    if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value))
                    {
                        return null;
                    }
                    obj3 = objA;
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    command.Dispose();
                    connection.Close();
                }
            }
            return obj3;
        }

        public static int ExecuteSqlInsertImg(string conn, string strSQL, byte[] fs)
        {
            int num2;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                SqlCommand command = new SqlCommand(strSQL, connection);
                SqlParameter parameter = new SqlParameter("@fs", SqlDbType.Image);
                parameter.Value = fs;
                command.Parameters.Add(parameter);
                try
                {
                    connection.Open();
                    num2 = command.ExecuteNonQuery();
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    command.Dispose();
                    connection.Close();
                }
            }
            return num2;
        }

        public static bool ExecuteSqlTran(string conn, ArrayList SQLStringList)
        {
            bool flag = false;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                connection.Open();
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                SqlTransaction transaction = connection.BeginTransaction();
                command.Transaction = transaction;
                try
                {
                    for (int i = 0; i < SQLStringList.Count; i++)
                    {
                        string str = SQLStringList[i].ToString();
                        if (str.Trim().Length > 1)
                        {
                            command.CommandText = str;
                            command.ExecuteNonQuery();
                        }
                    }
                    transaction.Commit();
                    flag = true;
                }
                catch
                {
                    transaction.Rollback();
                    flag = false;
                }
                finally
                {
                    connection.Close();
                    command.Dispose();
                    transaction.Dispose();
                }
                return flag;
            }
        }

        public static bool ExecuteSqlTran(string conn, Hashtable SQLStringList)
        {
            bool flag2;
            bool flag = false;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    SqlCommand cmd = new SqlCommand();
                    try
                    {
                        foreach (DictionaryEntry entry in SQLStringList)
                        {
                            string cmdText = entry.Key.ToString();
                            SqlParameter[] cmdParms = (SqlParameter[]) entry.Value;
                            PrepareCommand(cmd, connection, transaction, cmdText, cmdParms);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                        }
                        transaction.Commit();
                        flag = true;
                    }
                    catch
                    {
                        transaction.Rollback();
                        flag = false;
                        throw;
                    }
                    finally
                    {
                        connection.Close();
                        cmd.Dispose();
                        transaction.Dispose();
                    }
                    flag2 = flag;
                }
            }
            return flag2;
        }

        public static bool ExecuteSqlTran(string strconn, string SQLString)
        {
            string[] strArray = SQLString.Split(new char[] { ';' });
            ArrayList sQLStringList = new ArrayList();
            for (int i = 0; i < strArray.Length; i++)
            {
                sQLStringList.Add(strArray[i].ToString());
            }
            return ExecuteSqlTran(strconn, sQLStringList);
        }

        public static string[] GetAjaxCompletionList(string conn, string FieldName, string TableName, string prefixText, int countnum)
        {
            string str2;
            string connectionString = conn;
            bool flag = PageValidate.IsHasCHZN(prefixText);
            bool flag2 = PageValidate.IsNumberSign(prefixText);
            if (countnum == 0)
            {
                countnum = 10;
            }
            if (!flag && !flag2)
            {
                str2 = string.Format("SELECT Top {0} AjaxText FROM (SELECT DISTINCT AjaxText={1} from {2} where LOWER(dbo.getPY(Left({1},{4}))) = '{3}' group by dbo.getPY(Left({1},1)),{1}) AS vwAutoComplete", new object[] { countnum, FieldName, TableName, prefixText, prefixText.Length });
            }
            else
            {
                str2 = string.Format("SELECT Top {0} AjaxText FROM (SELECT DISTINCT AjaxText={1} from {2} where Left({1},{4}) = '{3}' group by {1}) AS vwAutoComplete", new object[] { countnum, FieldName, TableName, prefixText, prefixText.Length });
            }
            List<string> list = new List<string>(countnum);
            string cmdText = string.Format(str2, prefixText);
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand(cmdText, connection);
            SqlDataReader reader = null;
            try
            {
                connection.Open();
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    list.Add(reader.GetString(0));
                }
            }
            catch (SqlException exception)
            {
                throw new Exception(exception.Message);
            }
            finally
            {
                reader.Close();
                connection.Close();
                command.Dispose();
            }
            return list.ToArray();
        }

        public static object GetSingle(string conn, string SQLString)
        {
            object obj3;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                using (SqlCommand command = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        object objA = command.ExecuteScalar();
                        if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value))
                        {
                            return null;
                        }
                        obj3 = objA;
                    }
                    catch (SqlException exception)
                    {
                        throw new Exception(exception.Message);
                    }
                    finally
                    {
                        connection.Close();
                        command.Dispose();
                    }
                }
            }
            return obj3;
        }

        public static object GetSingle(string conn, string SQLString, params SqlParameter[] cmdParms)
        {
            object obj3;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                using (SqlCommand command = new SqlCommand())
                {
                    try
                    {
                        PrepareCommand(command, connection, null, SQLString, cmdParms);
                        object objA = command.ExecuteScalar();
                        command.Parameters.Clear();
                        if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value))
                        {
                            return null;
                        }
                        obj3 = objA;
                    }
                    catch (SqlException exception)
                    {
                        throw new Exception(exception.Message);
                    }
                    finally
                    {
                        connection.Close();
                        command.Dispose();
                    }
                }
            }
            return obj3;
        }

        public static string[] GetTableFieldName(string conn, string TableName)
        {
            using (SqlConnection connection = new SqlConnection(conn))
            {
                string[] strArray;
                SqlDataReader reader = null;
                SqlCommand command = null;
                ArrayList list = new ArrayList();
                try
                {
                    string str = "SELECT COLUMN_NAME,DATA_TYPE,IS_NULLABLE,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION            from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='";
                    command = new SqlCommand(str + TableName + "'", connection);
                    connection.Open();
                    reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                    while (reader.Read())
                    {
                        list.Add(reader.GetString(0));
                    }
                    strArray = new string[list.Count];
                    list.CopyTo(strArray);
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    reader.Close();
                    connection.Close();
                    command.Dispose();
                }
                return strArray;
            }
        }

        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
            {
                cmd.Transaction = trans;
            }
            cmd.CommandType = CommandType.Text;
            if (cmdParms != null)
            {
                foreach (SqlParameter parameter in cmdParms)
                {
                    if (((parameter.Direction == ParameterDirection.InputOutput) || (parameter.Direction == ParameterDirection.Input)) && (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(parameter);
                }
            }
        }

        public static DataSet Query(string conn, string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(conn))
            {
                DataSet dataSet = new DataSet();
                try
                {
                    connection.Open();
                    new SqlDataAdapter(SQLString, connection).Fill(dataSet, "ds");
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    connection.Close();
                }
                return dataSet;
            }
        }

        public static DataSet Query(string conn, string SQLString, params SqlParameter[] cmdParms)
        {
            DataSet set2;
            using (SqlConnection connection = new SqlConnection(conn))
            {
                SqlCommand cmd = new SqlCommand();
                PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                {
                    DataSet dataSet = new DataSet();
                    try
                    {
                        adapter.Fill(dataSet, "ds");
                        cmd.Parameters.Clear();
                    }
                    catch (SqlException exception)
                    {
                        throw new Exception(exception.Message);
                    }
                    finally
                    {
                        connection.Close();
                        cmd.Dispose();
                    }
                    set2 = dataSet;
                }
            }
            return set2;
        }

        public static DataSet Query(string conn, string SQLString, int Times)
        {
            using (SqlConnection connection = new SqlConnection(conn))
            {
                DataSet dataSet = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(SQLString, connection);
                    adapter.SelectCommand.CommandTimeout = Times;
                    adapter.Fill(dataSet, "ds");
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    connection.Close();
                }
                return dataSet;
            }
        }

        public static SqlDataReader RunProcedure(string conn, string storedProcName, IDataParameter[] parameters)
        {
            SqlConnection connection = new SqlConnection(conn);
            connection.Open();
            SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            command.CommandType = CommandType.StoredProcedure;
            return command.ExecuteReader();
        }

        public static DataSet RunProcedure(string conn, string storedProcName, IDataParameter[] parameters, string tableName)
        {
            using (SqlConnection connection = new SqlConnection(conn))
            {
                DataSet dataSet = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
                    adapter.Fill(dataSet, tableName);
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    connection.Close();
                }
                return dataSet;
            }
        }

        public static int RunProcedure(string conn, string storedProcName, IDataParameter[] parameters, out int rowsAffected)
        {
            using (SqlConnection connection = new SqlConnection(conn))
            {
                int num;
                SqlCommand command = null;
                try
                {
                    connection.Open();
                    command = BuildIntCommand(connection, storedProcName, parameters);
                    rowsAffected = command.ExecuteNonQuery();
                    num = (int) command.Parameters["ReturnValue"].Value;
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    connection.Close();
                    command.Dispose();
                }
                return num;
            }
        }

        public static DataSet RunProcedure(string conn, string storedProcName, IDataParameter[] parameters, string tableName, int Times)
        {
            using (SqlConnection connection = new SqlConnection(conn))
            {
                DataSet dataSet = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
                    adapter.SelectCommand.CommandTimeout = Times;
                    adapter.Fill(dataSet, tableName);
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    connection.Close();
                }
                return dataSet;
            }
        }

        public static DataSet TwoTableQuery(string conn, string strWhereA, string TableA, string TableB, string strWhereB, string ID)
        {
            using (SqlConnection connection = new SqlConnection(conn))
            {
                DataSet dataSet = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(strWhereA, connection);
                    SqlDataAdapter adapter2 = new SqlDataAdapter(strWhereB, connection);
                    adapter.Fill(dataSet, TableA);
                    adapter2.Fill(dataSet, TableB);
                    dataSet.Relations.Add("retions", dataSet.Tables[TableA].Columns[ID], dataSet.Tables[TableB].Columns[ID]);
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    connection.Close();
                }
                return dataSet;
            }
        }
    }
}

